﻿@namespace BootstrapBlazor.Components
@inherits CalendarBase

<div class="calendar">
    @if (ViewModel == CalendarViewModel.Month)
    {
        <div class="calendar-header">
            <div class="calendar-title">@GetTitle()</div>
            <div class="calendar-button-group">
                <div class="btn-group">
                    <button type="button" class="btn btn-sm" @onclick="e => OnChangeMonth(-1)">
                        <span>@PreviousMonth</span>
                    </button>
                    <button type="button" class="btn btn-sm" @onclick="e => OnChangeMonth(0)">
                        <span>@Today</span>
                    </button>
                    <button type="button" class="btn btn-sm" @onclick="e => OnChangeMonth(1)">
                        <span>@NextMonth</span>
                    </button>
                </div>
            </div>
        </div>
        <div class="calendar-body">
            <table cellspacing="0" cellpadding="0" class="calendar-table">
                <thead>
                    <tr>
                        @foreach (var w in WeekLists)
                        {
                            <th>@w</th>
                        }
                    </tr>
                </thead>
                <tbody>
                    @for (var week = StartDate; week < EndDate; week = week.AddDays(7))
                    {
                        <tr class="calendar-table-row">
                            @for (var index = 0; index < 7; index++)
                            {
                                @RenderCell(new CalendarCell { Value = week.AddDays(index), CurrentValue = Value })
                            }
                        </tr>
                    }
                </tbody>
            </table>
        </div>
    }
    else
    {
        <div class="calendar-header">
            <div class="calendar-title"><span class="d-none d-sm-inline-block">@GetTitle()</span> <span>@WeekNumberText</span></div>
            <div class="calendar-button-group">
                <div class="btn-group">
                    <button type="button" class="btn btn-sm" @onclick="e => OnChangeWeek(-7)">
                        <span>@PreviousWeek</span>
                    </button>
                    <button type="button" class="btn btn-sm" @onclick="e => OnChangeWeek(0)">
                        <span>@WeekText</span>
                    </button>
                    <button type="button" class="btn btn-sm" @onclick="e => OnChangeWeek(7)">
                        <span>@NextWeek</span>
                    </button>
                </div>
            </div>
        </div>
        <div class="calendar-body">
            <table cellspacing="0" cellpadding="0" class="calendar-table table-week">
                <thead>
                    <tr>
                        @for (var index = 0; index < 7; index++)
                        {
                            @RenderWeekHeader(index)
                        }
                    </tr>
                </thead>
                <tbody>
                    @ChildContent
                </tbody>
            </table>
        </div>
    }
</div>

@code {
    record CalendarCell
    {
        public DateTime Value { get; init; }
        public DateTime CurrentValue { get; init; }
    }

    private string? GetCalendarCellString(CalendarCell item) => CssBuilder.Default()
        .AddClass("prev", item.Value.Month < item.CurrentValue.Month)
        .AddClass("next", item.Value.Month > item.CurrentValue.Month)
        .AddClass("current", item.Value.Month == item.CurrentValue.Month)
        .AddClass("is-selected", item.Value.Ticks == item.CurrentValue.Ticks)
        .AddClass("is-today", item.Value.Ticks == DateTime.Today.Ticks)
        .Build();

    RenderFragment<CalendarCell> RenderCell => item =>
    @<td class="@GetCalendarCellString(item)" @onclick="@OnCellClickCallback(item.Value)">
        <div class="calendar-day">
            <span>@item.Value.Day</span>
        </div>
    </td>;

RenderFragment<int> RenderWeekHeader => index =>
@<th>
    <div class="@GetWeekDayClassString(index)">
        <div><span class="d-none d-sm-inline-block">@WeekHeaderText</span>@WeekLists.ElementAtOrDefault(index)</div>
        <div>@GetWeekDayString(index)</div>
    </div>
</th>;
}
